package com.example.dairenkun.neufoodshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.bean.Register;
import com.example.dairenkun.neufoodshop.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
         private Button btn_register;
         private EditText reg_username;
         private EditText reg_userphone;
         private EditText reg_password;
         private EditText reg_password2;
         private EditText reg_useraddres;
         private EditText reg_userremake;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvents();

    }
    private void initViews(){
        btn_register=(Button)findViewById(R.id.btn_reg_confirm);
        reg_username=(EditText)findViewById(R.id.reg_username);
        reg_userphone=(EditText)findViewById(R.id.reg_userphone);
        reg_password=(EditText)findViewById(R.id.reg_password);
        reg_password2=(EditText)findViewById(R.id.reg_password2);
        reg_useraddres=(EditText)findViewById(R.id.reg_useraddress);
        reg_userremake=(EditText)findViewById(R.id.reg_userremake);

    }
    private  void initEvents(){
        //点击注册
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!reg_password.getText().toString().equals(reg_password2.getText().toString()))
                {
                    Toast.makeText(RegisterActivity.this, "两次输入密码不一致！", Toast.LENGTH_SHORT).show();
                }else {
                    //1
                    UserModel userModel=new UserModel();
                    //2
                    Call call=userModel.Register(
                            reg_username.getText().toString(),
                            reg_password.getText().toString(),
                            reg_userphone.getText().toString(),
                            reg_useraddres.getText().toString(),
                            reg_userremake.getText().toString());
                    //3
                    Callback<Register>callback=new Callback<Register>() {
                        @Override
                        public void onResponse(Call<Register> call, Response<Register> response) {
                            String result=response.body().getSuccess();
                            if("0".equals(result))
                            {
                                //失败处理
                                Toast.makeText(RegisterActivity.this, "用户注册失败！", Toast.LENGTH_SHORT).show();
                            } else
                            {
                                //登录成功处理
                                Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                                String str_user=reg_username.getText().toString();
                                Intent intent=new Intent();
                                intent.putExtra("username",str_user);
                                setResult(RESULT_OK,intent);
                                finish();
                            }

                        }

                        @Override
                        public void onFailure(Call<Register> call, Throwable t) {
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    };
                    call.enqueue(callback);

                }


            }
        });

    }
}
